<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		hasEditor,
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				fileId:""
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods: {
			getWorkParam(){
				let _that = this
				if(_that.fileId){
					return {
						fileId:_that.fileId
					}
				}else{
					return ""
				}
			},
			formSubmit() {
				let _that = this
				_that.om.formSave("temployContractSign/addTemployContractSign", _that.formdata, _that)
			},
			initLoad(options) {
				let _that = this
				let fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					{
						url: "temployContractSign/setInfo",
					}
				]
				if (fileId) {
					_that.fileId = fileId
					ajaxArr.push({
						url: "temployContractSign/getInfo",
						param: {
							id: fileId
						}
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor("topinfo", "zposId", "data", configSelectData(res[0], "id", "posname"), mainData)
					if (!_that.edit) {
						if (res[1].value) {
							confirmModal(_that, "提示信息", "是否赋值需要签订合同的人员？", () => {
								_that.subEditorObj["title1"].workList = res[1].value
							}, () => {})
						}
					}
					if (_that.subConfigData['title1']) {
						var configData = _that.subConfigData['title1']
						var attr = {}
						attr.getWorkParam = "getWorkParam"
						if (hasEditor('title1', 'userId', configData)) {
							attr.titleTemplate = '姓名:<%= userName %>'
						}
						if (hasEditor('title1', 'sexName', configData)) {
							attr.subtitle1Template = '性别:<%= sexName %>'
						}
						if (hasEditor('title1', 'age', configData)) {
							attr.subtitle2Template = '年龄:<%= age %>'
						}
						changeEditor('title1', 'objStr', attr, mainData);
					}
					if (fileId) {
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for (let index in res[2].value.temployContractSign) {
							if (fieldArr.includes(index)) {
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata, index, res[2].value.temployContractSign[index])
								_that.$set(_that.formdata, index, res[2].value.temployContractSign[index])
							}
						}
						let dataList = _that._.clone(res[2].value.temployContractSign_datas)
						_that.subEditorObj["title1"].workList = dataList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "objStr", JSON.stringify(dataList))
						_that.$set(_that.formdata, "objStr", JSON.stringify(dataList))
						_that.$nextTick(function() {
							_that.$refs.autoform.initSelectIndex("topinfo", "zposId")
						})
					}
				}, options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onUnload() {
			//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				for (let fileId in _that.subEditorObj) {
					let editor = _that.subEditorObj[fileId]
					uni.getStorage({
						key: getWorkCacheKey(fileId),
						success: (res) => {
							editor.workList = res.data
							uni.removeStorage({
								key: getWorkCacheKey(fileId)
							})
						}
					})
				}
			}
		}
	}
</script>

<style></style>
